package com.kakarote.hrm.service.impl;

import com.kakarote.core.common.enums.SystemCodeEnum;
import com.kakarote.core.exception.CrmException;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.hrm.entity.PO.HrmEmployeeEncryptField;
import com.kakarote.hrm.mapper.HrmEmployeeEncryptFieldMapper;
import com.kakarote.hrm.service.IHrmEmployeeEncryptFieldService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 自定义员工加密字段表 服务实现类
 * </p>
 *
 * @author guomenghao
 * @since 2021-04-14
 */
@Service
public class HrmEmployeeEncryptFieldServiceImpl extends BaseServiceImpl<HrmEmployeeEncryptFieldMapper, HrmEmployeeEncryptField> implements IHrmEmployeeEncryptFieldService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer setFieldList(List<HrmEmployeeEncryptField> fieldList) {
        if (fieldList == null || fieldList.isEmpty()){
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        }
        // 删除所有
        int i = this.baseMapper.delete(null);
        if (i > 0){
            return this.baseMapper.insertBatchEmployeeEncryptField(fieldList);
        }
        return i;
    }

    @Override
    public Map<Integer, List<String>> fieldListGroupByType() {
        Map<Integer, List<String>> fieldGroup = new HashMap<>();
        List<HrmEmployeeEncryptField> list = this.lambdaQuery().eq(HrmEmployeeEncryptField::getIsEncrypt, 1).list();
        if (list != null && !list.isEmpty()) {
            fieldGroup = list.stream().collect(Collectors.groupingBy(HrmEmployeeEncryptField::getType, Collectors.mapping(HrmEmployeeEncryptField::getFieldName, Collectors.toList())));
        }
        return fieldGroup;
    }
}
